﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PRE.Catalog
{
    public partial class FrmXemdiem : PRE.Catalog.FrmBase
    {
        DAL.EntitesDAL.BangdiemDAL _dal = new DAL.EntitesDAL.BangdiemDAL();
        BLL.EntitiesBLL.BangdiemBLL _bll = new BLL.EntitiesBLL.BangdiemBLL();
        DAL.EntitesDAL.KhoahocDAL _dal1 = new DAL.EntitesDAL.KhoahocDAL();
        DAL.EntitesDAL.LophocDAL _dal2 = new DAL.EntitesDAL.LophocDAL();

        public FrmXemdiem()
        {
            InitializeComponent();
        }

        #region Override

        /// <summary>
        /// Perform when click add button
        /// </summary>
        protected override void PerformAdd()
        {
            base.PerformAdd();
        }

        /// <summary>
        /// Perform when click cancel button
        /// </summary>
        protected override void PerformCancel()
        {
            ChangeStatus_BD();
            PerformRefresh();
            base.PerformCancel();
        }

        /// <summary>
        /// Perform when click delete button
        /// </summary>
        /// <returns></returns>
        protected override void PerformDelete()
        {
            base.PerformDelete();
        }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected override void PerformEdit()
        {
            ReadOnlyControl(false);
            ChangeStatus_BD(false);
            txtDiemLT.Focus();
            base.PerformEdit();
        }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected override void PerformPrint()
        {
            base.PerformPrint();
        }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        protected override void PerformRefresh()
        {
            ReadOnlyControl();
            LoadData();
            ChangeStatus_BD();

            if (_dtb != null)
            {
                // Binding data
                ClearDataBindings();
                if (_dtb.Rows.Count > 0) DataBindingControl();
            }
            base.PerformRefresh();
        }

        /// <summary>
        /// Perform when click save button
        /// </summary>
        protected override void PerformSave()
        {
            var name = Proper(txtHocvien.Text);
            var id = grvMain.GetFocusedRowCellValue("Id") + "";
            var _mahv = grvMain.GetFocusedRowCellValue("MaHVID") + "";
            
            if (IsEdit)
            {            
                DAL.Entities.Bangdiem o = new DAL.Entities.Bangdiem()
                {
                    Id = Convert.ToInt64(id),
                    MaHVID = _mahv,
                    DiemLT = Convert.ToInt32(txtDiemLT.Text),
                    DiemTH = Convert.ToInt32(txtDiemTH.Text),
                    Xeploai = txtXeploai.Text,
                    Ghichu = txtGhichu.Text
                };

                var oki = _dal.Update(o);
                if (oki)
                {
                    // MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus_BD();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus_BD(false);
                    ReadOnlyControl(false);
                }
            }


            base.PerformSave();
        }

        /// <summary>
        /// Reset all input control
        /// </summary>
        protected override void ResetText()
        {
            base.ResetText();
        }

        /// <summary>
        /// Clear data binding
        /// </summary>
        protected override void ClearDataBindings()
        {
            txtHocvien.DataBindings.Clear();
            txtDiemLT.DataBindings.Clear();
            txtDiemTH.DataBindings.Clear();
            txtXeploai.DataBindings.Clear();
            txtGhichu.DataBindings.Clear();

            base.ClearDataBindings();
        }

        /// <summary>
        /// Add data binding
        /// </summary>
        protected override void DataBindingControl()
        {
            txtHocvien.DataBindings.Add("EditValue", _dtb, ".Hoten");
            txtDiemLT.DataBindings.Add("EditValue", _dtb, ".DiemLT");
            txtDiemTH.DataBindings.Add("EditValue", _dtb, ".DiemTH");
            txtXeploai.DataBindings.Add("EditValue", _dtb, ".Xeploai");
            txtGhichu.DataBindings.Add("EditValue", _dtb, ".Ghichu");

            base.DataBindingControl();
        }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only is trule else normal</param>
        protected override void ReadOnlyControl(bool isReadOnly = true)
        {
            //txtHocvien.Properties.ReadOnly = isReadOnly;
            txtDiemLT.Properties.ReadOnly = isReadOnly;
            txtDiemTH.Properties.ReadOnly = isReadOnly;
            txtXeploai.Properties.ReadOnly = isReadOnly;
            txtGhichu.Properties.ReadOnly = isReadOnly;

            //grcMain.Enabled = isReadOnly;

            base.ReadOnlyControl(isReadOnly);
        }

        protected override void PerformSearch()
        {
            base.PerformSearch();
        }

        /// <summary>
        /// Update object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool UpdateObject()
        {
            return base.UpdateObject();
        }

        /// <summary>
        /// Insert object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool InsertObject()
        {
            return base.InsertObject();
        }

        /// <summary>
        /// Load data
        /// </summary>
        protected override void LoadData()
        {
            DataTable tmp = _dal1.Select_Current(DateTime.Now);
            cboLophoc.Properties.DataSource = _dal2.Select(tmp.Rows[0]["Makhoa"] + "");
            cboLophoc.ItemIndex = -1;

            //_dtb = _dal.Select();
            
            //if (_dtb != null)
            //{
            //    grcMain.DataSource = _dtb;
            //}

            base.LoadData();
        }
        #endregion

        private void FrmXemdiem_Load(object sender, EventArgs e)
        {
            PerformRefresh();
        }

        //private void cboKhoahoc_EditValueChanged(object sender, EventArgs e)
        //{
        //    cboLophoc.Properties.ReadOnly = false;
        //    string id = cboKhoahoc.EditValue + "";
        //    cboLophoc.Properties.DataSource = _dal2.Select(id);
        //    cboLophoc.ItemIndex = -1;

        //    DataTable tmp = _dal.Select_Xemdiem(id);

        //    if (tmp != null)
        //        grcMain.DataSource = tmp;

        //}

        private void cboLophoc_EditValueChanged(object sender, EventArgs e)
        {
            DataTable tmp = _dal1.Select_Current(DateTime.Now);
            string _makhoa = tmp.Rows[0]["Makhoa"] + "";
            string _malop = cboLophoc.EditValue + "";
            _dtb = _dal.Select_Xemdiem(_makhoa, _malop);

            
            if (_dtb != null)
            {
                grcMain.DataSource = _dtb;
            }

            if (_dtb != null)
            {
                // Binding data
                ClearDataBindings();
                if (_dtb.Rows.Count > 0) DataBindingControl();
            }
        }

        private void txtDiemLT_Leave(object sender, EventArgs e)
        {
            float _lt = Convert.ToInt32(txtDiemLT.Text);
            float _th = Convert.ToInt32(txtDiemTH.Text);

            string result = _bll.Rank(_lt, _th);
            txtXeploai.Text = result;
            txtGhichu.Text = result == "Rớt" ? "H" : "Đ";  
        }

        private void txtDiemTH_Leave(object sender, EventArgs e)
        {
            float _lt = Convert.ToInt32(txtDiemLT.Text);
            float _th = Convert.ToInt32(txtDiemTH.Text);

            string result = _bll.Rank(_lt, _th);
            txtXeploai.Text = result;
            txtGhichu.Text = result == "Rớt" ? "H" : "Đ";
        }

    }
}
